package me.zhengjie.controller.PPInBoundController;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.pojo.sqlServer.PPInboundMission;
import me.zhengjie.pojo.sqlServer.PPInfo;
import me.zhengjie.ppService.PPInBoundService;
import me.zhengjie.ppService.PPInventoryService;
import me.zhengjie.ppService.PpPalletService;
import me.zhengjie.pojo.InBoundInfo;
import me.zhengjie.pojo.Resp;
import me.zhengjie.repository.PPInboundMissionRepository;
import me.zhengjie.repository.PPInfoRepository;
import me.zhengjie.service.PPInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "入库管理")
@RequestMapping("/api/ppInBound")
public class PPInBoundController {

    private static final Logger log = LoggerFactory.getLogger("pp");
    private final PPInfoService ppInfoService;

    private final PpPalletService ppPalletService;
    private final PPInventoryService ppInventoryService;
    private final PPInBoundService ppInBoundService;



    @Log("保存pp信息和生成入库任务")
    @ApiOperation("保存pp信息和生成入库任务")
    @PostMapping("/saveInBoundInfo")
    public Resp saveInBoundInfo(@RequestBody InBoundInfo inBoundInfo) {
        try {
            if (ppInBoundService.ifExistPallet(inBoundInfo.getPalletCode())){
                return Resp.builder().status(503).msg("已存在该载盘的入库任务").build();
            }

            //校验一下数据
            String msg = ppInfoService.validatePPInfo(inBoundInfo);
            if (msg != null) {
                return Resp.builder().status(500).msg(msg).build();
            }

            return ppInfoService.saveOrUpdate(inBoundInfo);
        } catch (Exception e) {
            return Resp.builder().status(500).msg("生成入库任务失败"+e.getMessage()).build();
        }
    }

    //载盘回库操作，只要载盘码
    @Log("通过pp信息来查pp库判断是否已经存在")
    @ApiOperation("通过pp信息来查pp库判断是否已经存在")
    @GetMapping("/isExistInventory/{ppBatch}")
    public Resp isExist(@PathVariable String ppBatch){
        try {
            if (ppInventoryService.isExist(ppBatch)) {
                return Resp.builder().data("display", 1).status(200).msg("pp回库操作,需要弹窗").build();
            } else {
                return Resp.builder().data("display", 0).status(201).msg("pp首次入库,无需弹窗").build();
            }
        } catch (Exception e) {
            return Resp.builder().status(500).msg("查询失败").build();
        }
    }

    @Log("plc获取载盘码")
    @ApiOperation("plc获取载盘码")
    @GetMapping("/getPalletCode/{stationCode}")
    public Resp getPalletCode(@PathVariable String stationCode) {
        try {
            String palletCode = ppPalletService.getPalletCode(stationCode);

            return Resp.builder().data("palletCode", palletCode).status(200).msg("查询成功").build();
        } catch (Exception e) {
            return Resp.builder().status(500).msg("查询失败").build();
        }
    }


    @Log("随机获取库内盘子批号")
    @ApiOperation("plc获取载盘码")
    @GetMapping("/getRandom")
    public List<String> getPalletCode() {

        //先查库存
        List<String> list = ppInventoryService.getPpBatchRandom();
        return list;

    }



}
